<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>借阅历史</title>
    <script type="text/javascript" src="jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="jquery.cookie.js"></script>
    <link rel="icon" href="images/lenthistory.png" type="img/x-ico" />
</head>

<body>

<ul>
    <li><a class="active" href="Manager.html">返回上一页</a></li>
    <li><a href="">借阅历史</a></li>
</ul>
<h1>借阅历史：</h1>
<div class="search">
    <input type="text" id="sou" placeholder="搜索">
    <input type="text" id="userId" placeholder="输入读者id" >
    <input type="text" id="bookId" placeholder="输入图书id" >
    <input type="text" id="bookName" placeholder="输入图书名称" ><br>
    <input type="button" id="sure" value="查询" >
</div>
<div id="t">
    <table id="table" class="mytable" border="2" bgcolor="#f0f8ff"></table>
</div>
</body>

</html>

<style>
    body {
        margin: 0px;
        padding: 0px;
        font-family: sans-serif;
        /*设置字体为sans-serif*/
        background-image: url(images/library.jpg);
        background-size: 100% 100%;
        background-attachment: fixed;
        /*背景图片尺寸为覆盖cover*/
    }

    h1{
        text-align: center;
    }

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
    }

    li {
        float: left;
    }

    li a,
    .dropbtn {
        display: inline-block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }

    li a:hover,
    .user:hover,
    .dropdown:hover {
        background-color: #111;
        /*鼠标移动时背景颜色*/
    }

    #sou {
        background-image: url('images/searchicon.png');
        /* 搜索按钮图标 */
        background-position: 5px 1px;
        /* 定位搜索按钮 */
        background-repeat: no-repeat;
        /* 不重复图片*/
        width: 100%;
        font-size: 20px;
        /* 提示文字大小 */
        padding: 12px 20px 12px 20px;
        border: 4px solid #ddd;
        margin-bottom: 12px;
    }

    .search {
        width: 20%;
        margin: 50px auto;
        margin-left: 450px;
        /*搜索栏的位置*/
        display: flex;
        /**/
    }

    .search input {
        float: left;
        flex: 4;
        height: 30px;
        outline: none;
        border: 1px solid white;
        box-sizing: border-box;
        padding-left: 10px;
    }

    .search button {
        float: right;
        flex: 1;
        height: 30px;
        background-color: rgb(63, 224, 245);
        color: white;
        border-style: none;
        outline: none;
    }

    .search button i {
        font-style: normal;
    }

    .search button:hover {
        font-size: 20px;
        background-color: rgb(63, 224, 245);
    }

    #sure{
        text-align: center;
        background: transparent;
        border: none;
        outline: none;
        font-size: 15px;
        color: #03a9f4;
        background: rgb(190, 221, 247);
        padding: 4px 4px;
        cursor: pointer;
        /*光标呈现为指示链接的指针（一只手）*/
        border-radius: 10px;
        position: relative;
        top: auto;
    }

    #t{
        padding-left:26%;
    }
    mytable {
        width: 500px;
        border: 2px;
        text-align: center;
        border-collapse: collapse;
        background-color: cornsilk;
        padding-left:20%;
    }

    .mytable tr {
        width: 100px;
        padding-left:15%;
    }
</style>
<script>
    var mBookName = ""
    $("#sure").click(function () {
        var workerFindHistory  = {
            bookName:$("#bookName").val(),
            userId:$("#userId").val(),
            bookId:$("#bookId").val()
        }
        var html = "<tr><th>图书ID</th><th>图书名称</th><th>用户ID</th><th>借出日期</th><th>应还日期</th><th>实际归还日期</th><th>借阅图书状态</th>"
        $.ajax({
            url: "http://localhost:8080/borrow/findHistory",
            type: "post",
            dataType: 'json',
            data: JSON.stringify(workerFindHistory ),
            contentType: "application/json",
            success: function (res) {
                alert(res.msg)
                for(var i=0;i<res.data.length;i++) {
                    var id = res.data[i].bookId
                    var data = res.data[i].realDate
                    F(id);
                    if (data == null) {
                        data = "无"
                        html = html +"<tr><td>" +res.data[i].bookId+"</td><td>"+mBookName+"</td><td>"+res.data[i].userId+"</td><td>"+
                            dateformat(res.data[i].borrowDate)+"</td><td>"+dateformat(res.data[i].shouldDate)+"</td><td>"+data+"</td><td>"+res.data[i].borrowStatus+"</td></tr>";
                    }else {
                        html = html +"<tr><td>" +res.data[i].bookId+"</td><td>"+mBookName+"</td><td>"+res.data[i].userId+"</td><td>"+
                            dateformat(res.data[i].borrowDate)+"</td><td>"+dateformat(res.data[i].shouldDate)+"</td><td>"+dateformat(res.data[i].realDate)+"</td><td>"+res.data[i].borrowStatus+"</td></tr>";
                    }
                }
                $("#table").html(html)
            },
            error: function (res) {
                alert("JSON数据获取失败，请联系管理员！");
            }
        })
    })

    function dateformat(str){
        if(str == null || str == "" || typeof(str)=="undefined"){
            return "";
        }
        var now = new Date(str);
        var year = now.getFullYear();
        var month =(now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year +"-"+ month +"-"+  day;
        return dateTime;
    }
    function F(id){
        var bookware = {
            bookWareId:id,
            bookName:"",
            bookAuthor:"",
            bookPublisher:"",
            bookType:""
        }
        $.ajax({
            url:"http://localhost:8080/bookware/findbookware",
            type:"post",
            async: false,
            dataType: 'json',
            data: JSON.stringify(bookware),
            contentType: "application/json",
            success:function (res) {
                console.log(res.msg)
                if(res.msg=="查询图书成功") {
                    mBookName=res.data[0].bookName
                }
            },
            error: function (res) {
                alert("JSON数据获取失败，请联系管理员！");
            }
        })
    }
</script>
